<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('添加用户')" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-staff-resetPwd">
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">手机号：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text"   name="mobile" id="mobile" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">输入密码：</label>
				<div class="col-sm-8">
					<input class="form-control" type="password" name="password" id="password" >
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">用户昵称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text"   name="username" id="username" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">真实姓名：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text"   name="realName" id="realName" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">性别：</label>
				<div class="col-sm-8">
					<select id="gender" name="gender" class="form-control m-b">
						<option value="0">女</option>
						<option value="1">男</option>
					</select>
				</div>
			</div>

		</form>
	</div>
	<th:block th:include="include :: footer" />
	<script th:src="@{/js/md5.js}"></script>
	<script type="text/javascript">
		// 注册自定义密码复杂度验证方法
		$.validator.addMethod("passwordComplexity", function(value, element) {
			// 至少一个大写字母
			var hasUpperCase = /[A-Z]/.test(value);
			// 至少一个小写字母
			var hasLowerCase = /[a-z]/.test(value);
			// 至少一个数字
			var hasNumbers = /\d/.test(value);
			// 至少一个特殊字符
			var hasSpecial = /[^A-Za-z\d]/.test(value);

			// 满足所有条件返回true
			return this.optional(element) || (hasUpperCase && hasLowerCase && hasNumbers && hasSpecial);
		}, "密码必须包含大小写字母、数字和特殊字符");
		$("#form-staff-resetPwd").validate({
			rules:{
				password:{
					required:true,
					minlength: 8,
					maxlength: 20,
					passwordComplexity: true // 添加自定义复杂度校验
				},
				mobile:{
					required:true,
					minlength: 11,
					maxlength: 11
				},
			},
			focusCleanup: true
		});

		var isClick = true;
		function submitHandler() {
			if ($.validate.form() && isClick) {
				isClick = false;
				create();
	        }
	    }
		function create() {
			var mobile = $("#mobile").val();
			var password = md5($("#password").val());
			var username = $("#username").val();
			var realName = $("#realName").val();
			var gender = $("#gender").val();

			var staff = {
				"mobile": mobile,
				"password": password,
				"username": username,
				"realName": realName,
				"gender": gender,
			}
			$.ajax({
				type: "post",
				url: "/sso/staff/create",
				headers: {
					'Authorization': "",
				},
				contentType: "application/json; charset=utf-8",
				data: JSON.stringify(staff),
				dataType: 'json',
				success: function (data) {
					if (data.code == web_status.SUCCESS) {
						setTimeout(() => {
							parent.location.reload();
							$.modal.close();
						}, 1000);
					} else {
						$.modal.alertError(data.msg);
					}
				}
			});
		}
	</script>
</body>

</html>
